﻿using AnimeLibrary.App_Code;
using System;
using System.Data;
using System.Text;
using System.Xml;

namespace AnimeLibrary
{
    public partial class AnimeLibraryRSS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                PostRSS();
        }

        private void PostRSS()
        {
            XmlTextWriter feedWriter = null;

            try
            {
                string pageTitle = "/rss_feeds.aspx";
                string siteUrl = Request.Url.ToString().ToLower().Replace(pageTitle, string.Empty);

                // Clear any previous output from the buffer
                Response.Clear();
                Response.ContentType = "text/xml";

                DateTime pubDate = DateTime.Now;

                string commandText = "SELECT * FROM tbl_anime_library ORDER BY [Title] ASC";
                DataTable dt = ReadOp.GetDataTable(Request.MapPath("App_Data\\AnimeLibrary.accdb"), commandText);

                feedWriter = new XmlTextWriter(Response.OutputStream, Encoding.ASCII);

                feedWriter.WriteStartDocument();

                // These are RSS Header Tags
                feedWriter.WriteStartElement("rss");
                feedWriter.WriteAttributeString("version", "2.0");
                feedWriter.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");
                //feedWriter.WriteAttributeString("xmlns:media", "http://search.yahoo.com/mrss/");

                feedWriter.WriteStartElement("channel");

                feedWriter.WriteElementString("title", "Anime Online Library");
                feedWriter.WriteElementString("link", siteUrl);
                feedWriter.WriteElementString("description", "Keep up-to-date with all your favorite Anime series");
                feedWriter.WriteElementString("category", "Anime Series");

                feedWriter.WriteStartElement("atom:link");
                feedWriter.WriteAttributeString("href", Request.Url.ToString());
                feedWriter.WriteAttributeString("rel", "self");
                feedWriter.WriteAttributeString("type", "application/rss+xml");
                feedWriter.WriteEndElement();

                foreach (DataRow dr in dt.Rows)
                {
                    feedWriter.WriteStartElement("item");

                    feedWriter.WriteElementString("title", dr["Title"].ToString());
                    feedWriter.WriteElementString("link", string.Format("{0}/details.aspx?item={1}", siteUrl, dr["ID"].ToString()));
                    feedWriter.WriteElementString("description", string.Format("{0}<br/><br/><img src=\"{1}/{2}\" />", dr["PlotSummary"].ToString(), siteUrl, dr["ThumbnailLocation"].ToString()));
                    feedWriter.WriteElementString("guid", string.Format("{0}/details.aspx?item={1}", siteUrl, dr["ID"].ToString()));
                    feedWriter.WriteElementString("category", "Anime Series");
                    feedWriter.WriteElementString("pubDate", string.Format("{0:r}", pubDate));

                    //feedWriter.WriteStartElement("media:content");
                    //feedWriter.WriteAttributeString("url", string.Format("{0}/{1}", siteUrl, dr["ThumbnailLocation"].ToString()));
                    //feedWriter.WriteAttributeString("medium", "image");
                    //feedWriter.WriteEndElement();

                    feedWriter.WriteStartElement("enclosure");
                    feedWriter.WriteAttributeString("url", string.Format("{0}/{1}", siteUrl, dr["ImageLocation"].ToString()));
                    feedWriter.WriteAttributeString("type", string.Format("image/{0}", dr["ImageLocation"].ToString().Substring(dr["ImageLocation"].ToString().LastIndexOf('.') + 1)));
                    feedWriter.WriteAttributeString("length", "0");
                    feedWriter.WriteEndElement();

                    feedWriter.WriteEndElement();
                }

                feedWriter.WriteEndElement();
                feedWriter.WriteEndElement();
                feedWriter.WriteEndDocument();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                if (feedWriter != null)
                {
                    feedWriter.Flush();
                    feedWriter.Close();
                }

                feedWriter = null;

                Response.End();
            }
        }
    }
}